package com.sanwei.taskmanagement.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {

    // 设置默认过期时间
    private static final long JWT_TTL = 1000 * 60 * 60;

    // jwt密钥，服务器私有
    private static final String JWT_KEY = "abcdefjhigklmnopqrstuvwxyz123456";

    // 生成token
    public static String generateToken(String username) {
        Date expTime = new Date(System.currentTimeMillis() + JWT_TTL);
        return Jwts.builder()
                // iss (issuer)：签发人
                .setIssuer("SanWei")
                // sub (subject)：主题
                .setSubject(username)
                // aud (audience)：受众
//                .setAudience()
                // iat (Issued At)：签发时间
                .setIssuedAt(new Date())
                // exp (expiration time)：过期时间
                // 一小时后过期
                .setExpiration(expTime)
                // nbf (Not Before)：生效时间
//                .setNotBefore(new Date(System.currentTimeMillis()))
                // 签名
                .signWith(SignatureAlgorithm.HS256, JWT_KEY)
                .compact();
    }

    // 解析token
    public static Claims getClaimsVyToken(String token) {
        return Jwts.parser()
                .setSigningKey(JWT_KEY)
                .parseClaimsJws(token)
                .getBody();
    }

//    public static void main(String[] args) {
//        String token = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJTYW5XZWkiLCJzdWIiOiJjeSIsImlhdCI6MTc0MjkwODYyMCwiZXhwIjoxNzQyOTEyMjIwfQ.CB-MaYjwbJKp4OT5cZODV0ndmc6fUMV8luEZSyeEiR4";
//        Claims claimsVyToken = getClaimsVyToken(token);
//        System.out.println(claimsVyToken.toString());
//    }
}
